package com.ruoyi.business.controller;

import com.ruoyi.business.domain.PressureFlowData;
import com.ruoyi.business.service.IPressureFlowDataService;
import com.ruoyi.business.vo.AirPressureGroupVO;
import com.ruoyi.business.vo.ExperimentTimeRangeVO;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 压力流量数据Controller
 * 
 * @author qiancheng
 * @date 2025-09-16
 */
@RestController
@RequestMapping("/business/flowData")
public class PressureFlowDataController extends BaseController
{
    @Autowired
    private IPressureFlowDataService pressureFlowDataService;

    /**
     * 查询压力流量数据列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PressureFlowData pressureFlowData)
    {
        startPage();
        List<PressureFlowData> list = pressureFlowDataService.selectPressureFlowDataList(pressureFlowData);
        return getDataTable(list);
    }

    /**
     * 导出压力流量数据列表
     */
    @PreAuthorize("@ss.hasPermi('business:data:export')")
    @Log(title = "压力流量数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PressureFlowData pressureFlowData)
    {
        List<PressureFlowData> list = pressureFlowDataService.selectPressureFlowDataList(pressureFlowData);
        ExcelUtil<PressureFlowData> util = new ExcelUtil<PressureFlowData>(PressureFlowData.class);
        util.exportExcel(response, list, "压力流量数据数据");
    }

    /**
     * 获取压力流量数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pressureFlowDataService.selectPressureFlowDataById(id));
    }

    /**
     * 新增压力流量数据
     */
    @PreAuthorize("@ss.hasPermi('business:data:add')")
    @Log(title = "压力流量数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PressureFlowData pressureFlowData)
    {
        return toAjax(pressureFlowDataService.insertPressureFlowData(pressureFlowData));
    }

    /**
     * 修改压力流量数据
     */
    @PreAuthorize("@ss.hasPermi('business:data:edit')")
    @Log(title = "压力流量数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PressureFlowData pressureFlowData)
    {
        return toAjax(pressureFlowDataService.updatePressureFlowData(pressureFlowData));
    }

    /**
     * 删除压力流量数据
     */
    @PreAuthorize("@ss.hasPermi('business:data:remove')")
    @Log(title = "压力流量数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pressureFlowDataService.deletePressureFlowDataByIds(ids));
    }

    /**
     * 下载压力流量 Excel 模板
     */
    @Anonymous
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) throws Exception {
        pressureFlowDataService.downloadTemplate(response);
    }


    /**
     * 导入压力流量数据
     * 解析 Excel 文件，并调用 Service 保存数据，统一使用导入时间参数
     *
     * @param file Excel 文件
     * @param importTime 导入时间，由前端传入，格式 yyyy-MM-dd HH:mm
     * @return 导入结果信息
     * @throws Exception 导入或解析异常
     */
    @Anonymous
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file,
                                 @RequestParam("importDay")
                                 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") Date importDay,
                                 @RequestParam("startTime") String startTime,
                                 @RequestParam("endTime") String endTime) throws Exception {
        return pressureFlowDataService.importPressureFlowData(file, importDay,startTime,endTime);
    }

    /**
     * 获取导入时间列表
     */
    @Anonymous
    @GetMapping("/importTimes")
    public AjaxResult getImportTimeList() {
        List<String> timeList = pressureFlowDataService.getImportTimeList();
        return AjaxResult.success(timeList);
    }

    @Anonymous
    @ApiOperation("根据导入时间获取压力流量实验时间范围")
    @GetMapping("/timeRanges")
    public AjaxResult getPressureFlowTimeRanges(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date importDay) {
        List<ExperimentTimeRangeVO> ranges = pressureFlowDataService.getPressureFlowTimeRanges(importDay);
        return AjaxResult.success(ranges);
    }


    /**
     * 获取按气压分组的图表数据（核心接口）
     * 从Service层实现分组逻辑，不依赖XML的resultMap
     * @return 气压分组VO列表
     */
    @Anonymous
    @GetMapping("/getChartData")
    public AjaxResult getChartData(@RequestParam("importDay")
                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                   Date importTime,
                                   String startTime,
                                   String endTime) {
        List<AirPressureGroupVO> data = pressureFlowDataService.getChartDataGroupedByAirPressure(importTime,
                startTime,
                endTime);
        return AjaxResult.success(data);
    }


}
